#include "ReutersDocument.hpp"




ReutersDocument ReutersDocument::NULL_DOC ("", "");


ReutersDocument::ReutersDocument(QString file, QString title) :
    loaded_ (false),
    file_ (file),
    title_ (title)
{}


int ReutersDocument::rowCount (const QModelIndex&) const {
    if (!loaded())
        return 1;
    else
        return 5;
}


int ReutersDocument::columnCount (const QModelIndex&) const {
    if (!loaded())
        return 1;
    else
        return 2;
}


QVariant ReutersDocument::data (const QModelIndex & index, int role) const {
    if (role == Qt::DisplayRole) {
        if (!loaded()) {
            return "Nie wybrano żadnej depeszy.";
        }
        switch (index.row()) {
        case 0:
            if (index.column() == 0) {
                return "Data:";
            } else {
                return dateline_;
            }
            break;
        case 1:
            if (index.column() == 0) {
                if (topics_.count() != 1)
                    return "Kategorie (oryginalne):";
                else
                    return "Kategoria (oryginalna):";
            } else {
                QString all_topics;
                bool first = true;
                for (QString topic : topics_) {
                    all_topics += (first ? "" : ", ") + topic;
                    first = false;
                }
                return all_topics;
            }
            break;
        case 2:
            if (index.column() == 0) {
                if (topics_.count() != 1)
                    return "Kategorie (z klasyfikatora):";
                else
                    return "Kategoria (z klasyfikatora):";
            } else {
                QString all_assigned_topics;
                bool first = true;
                for (QString topic : assignedTopics_) {
                    all_assigned_topics += (first ? "" : ", ") + topic;
                    first = false;
                }
                return all_assigned_topics;
            }
            break;
        case 3:
            if (index.column() == 0) {
                return "Tytuł:";
            } else {
                return title_;
            }
            break;
        case 4:
            if (index.column() == 0) {
                return "Zawartość:";
            } else {
                return body_;
            }
            break;
        }
    } else if (role == Qt::TextAlignmentRole) {
        if (index.row() == 4 || !loaded())
            return Qt::AlignTop;
    }
    return QVariant ();
}


QVariant ReutersDocument::headerData (int section, Qt::Orientation orientation, int role) const {
    if (role == Qt::DisplayRole && orientation == Qt::Horizontal && loaded()) {
        if (section == 0) {
            return "Atrybut:";
        } else if (section == 1) {
            return "Wartość:";
        }
    }
    return QVariant ();
}

